/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import net.groupme.model.MODEL_CONSTANTS;

/**
 * created on 17 Dec 2007
 * 
 * This class provides some utility functions for web resources, e.g. detecting the content type of a resource.
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.2 $ $Date: 2008-06-12 12:17:33 $
 */
public class URLUtility {

	/**
	 * Detects the content type of the resource identified by the given URL.
	 * @param url the url 
	 * @return type indicator, e.g. MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE
	 */
	public static final String detectContentType(String url){
		URL realUrl = null;
		try {
			realUrl = new URL(url);
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		}
		String contentType = "";
		try {
			if(realUrl != null){
				contentType = realUrl.openConnection().getContentType();
//				image
				if (contentType.startsWith("image")){
					return MODEL_CONSTANTS.RESOURCE_TYPE_IMAGE;
				}
				//video
				else if (contentType.startsWith("video")){
					return MODEL_CONSTANTS.RESOURCE_TYPE_VIDEO;
				}
				//audio
				else if (contentType.startsWith("audio")){
					return MODEL_CONSTANTS.RESOURCE_TYPE_AUDIO;
				}
				//application
				else if ("application/x-shockwave-flash".equals(contentType)){
					return MODEL_CONSTANTS.RESOURCE_TYPE_SHOCKWAVE;
				}else{
					return MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE;
				}
				//TODO: add also other types like RSS, Publication
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE;
	}
}
